<?php
session_start();
//1.创建图像资源
$imgRs = imagecreatetruecolor(60, 30);
//2.设置颜色
$imgBgColor = imagecolorallocate($imgRs, rand(0, 255), rand(0, 255), rand(0, 255));
//3.填充
imagefill($imgRs, 0, 0, $imgBgColor);

//4.写入文字，语法：imagestring（图像资源，字体大小，x，y，内容，颜色）
$fontSize = 10;
$x = 13;
$y = 8;
$content = getNum();
$_SESSION['captcha'] = $content;
$fontColor = imagecolorallocate($imgRs, rand(0, 255), rand(0, 255), rand(0, 255));
imagestring($imgRs, $fontSize, $x, $y, $content, $fontColor);

header('content-type:image/jpeg');
//5.输出
imagejpeg($imgRs);

function getNum() {
	$str = '0123456789';
	$str = str_shuffle($str);
	return substr($str, 0, 4);
}